package com.spring.data.jdbc.test;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Slf4j
public class GetCMSKindInfo {

	public static Vector<String> ipVector = new Vector<String>();
	public static Map<String ,List<String>> infoMap= new HashMap<String, List<String>>();
	public static int rowNum = 2;
	
	static Random random = new Random();
	static int randomRound = 100;
	final static int key2 = random.nextInt(randomRound);
	
	
	public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            //System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
            	ipVector.add(tempString.trim());
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
	
	public static void main(String[] args) {
		 infoMap = new HashMap<String, List<String>>();
		 String fileName="D:\\Desktop\\1\\CinemaInfo.txt";
         
         
		GetCMSKindInfo.readFileByLines("D:\\Desktop\\1\\oip.txt");

		log.info("GetCMSKindInfo.ipVector.size = {}", GetCMSKindInfo.ipVector.size());

//		System.out.println("查询开始........请等待结束提示");
//		bulidInfo();
//		System.out.println("查询结束........窗口会自动关闭");
//		
		System.out.println("查询开始........请等待结束提示");
//		bulidInfo();
		ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);
		for(int i = 1; i <= 20; i++) {
			ses.execute(new Command3w());
		}
		ses.shutdown();
		System.out.println("查询结束........窗口会自动关闭");
		
		try {
			Thread.sleep(3000);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	public static void bulidInfo() {

		while (!GetCMSKindInfo.ipVector.isEmpty()) {
			String ip = GetCMSKindInfo.ipVector.remove(0);
			 
			String dirverClassName = "com.mysql.jdbc.Driver";
			String url_1 = "jdbc:mysql://";
			String url_2 = ":3306/CMS?useUnicode=true&characterEncoding=utf8";
			String user = "cpms";
			String password = "udqjHDMkxQfGP4iy";
			Connection conn = null;
			// 初始化JDBC驱动并让驱动加载到jvm中
			try {
				Class.forName(dirverClassName);
			} catch (ClassNotFoundException e) {
				//e.printStackTrace();
			}
			// 创建数据库连接
			boolean connect = true;
			try {
				conn = DriverManager.getConnection(url_1 + ip + url_2, user, password);
			} catch (SQLException e) {
				connect = false;
				//e.printStackTrace();
			}
			//String sql = "SELECT C.`CODE`, C.`NAME`,C.UID FROM CI_CINEMA C";
			 
			String sql = "SELECT \r\n" + 
					"	(SELECT NAME FROM CI_CINEMA LIMIT 1) AS '影院',\r\n" + 
					"TYPE_CODE,\r\n" + 
					"TYPE_NAME\r\n" + 
					"\r\n" + 
					"FROM `POS_PAY_TYPE`\r\n" + 
					"WHERE TYPE_CODE LIKE 'JD%' OR TYPE_NAME LIKE '京东%'\r\n" + 
					";";
		    PreparedStatement pstmt;
		    StringBuffer sb = new StringBuffer();
		    try {
		    	 boolean f =false;
		    	if(connect) {
		    		pstmt = (PreparedStatement)conn.prepareStatement(sql);
		    		ResultSet rs = pstmt.executeQuery();
		    		int col = rs.getMetaData().getColumnCount();
		    		while (rs.next()) {
		    			sb.append(ip + "\t");
		    			for (int i = 1; i <= col; i++) {
		    				 sb.append("|");
		    				if(i != col) {
		    					sb.append(rs.getString(i) + "\t");
		    				}else {
		    					sb.append(rs.getString(i) + "");
		    				}
		    			}
		    			
		    			f = true;
		    		}
		    		if(!f) {
		    			sb.append(ip + "\t");
		    			sb.append("(未开启)" + "\t");
		    		}
		    	}else {
		    		sb.append(ip + "\t");
		    		sb.append("连接失败");
		    	}
		    } catch (SQLException e) {
		         e.printStackTrace();
		    }
		    
		    System.out.println(sb.toString());
		}
	}
	
	 
	
	
}



class Command3w implements Runnable{

	@Override
	public void run() {
		ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);
		for(int i = 1; i <= 20; i++) {
			ses.execute(new RunPrint3w(""));
		}
		ses.shutdown();
	}
	
}
class RunPrint3w implements Runnable{

	private String key;
	public RunPrint3w(String k){
		this.key = k;
	}
	
	@Override
	public void run() {
		while (!GetCMSKindInfo.ipVector.isEmpty()) {
			String ip = GetCMSKindInfo.ipVector.remove(0);
			StringBuffer sb = new StringBuffer();
			sb.append(ip + "\t");
			String dirverClassName = "com.mysql.cj.jdbc.Driver";
			String url_1 = "jdbc:mysql://";
			String url_2 = ":3306/CMS?useUnicode=true&characterEncoding=utf8";
			String user = "upss";
			String password = "Ticketupss";
			Connection conn = null;
			// 初始化JDBC驱动并让驱动加载到jvm中
			try {
				Class.forName(dirverClassName);
			} catch (ClassNotFoundException e) {
				//e.printStackTrace();
			}
			// 创建数据库连接
			boolean connect = true;
			try {
				conn = DriverManager.getConnection(url_1 + ip + url_2, user, password);
			} catch (SQLException e) {
				connect = false;
				//e.printStackTrace();
			}
			String sql = "SELECT \r\n" + 
					"	(SELECT NAME FROM CI_CINEMA LIMIT 1) AS '影院',\r\n" + 
					"TYPE_CODE,\r\n" + 
					"TYPE_NAME\r\n" + 
					"\r\n" + 
					"FROM `POS_PAY_TYPE`\r\n" + 
					"WHERE TYPE_CODE LIKE 'JD%' OR TYPE_NAME LIKE '京东%'\r\n" + 
					";";
		    PreparedStatement pstmt;
		    try {
		    	if(connect) {
		    		pstmt = (PreparedStatement)conn.prepareStatement(sql);
		    		ResultSet rs = pstmt.executeQuery();
		    		int col = rs.getMetaData().getColumnCount();
		    		while (rs.next()) {
						sb.append("查询");
		    			for (int i = 1; i <= col; i++) {
		    				sb.append("|");
		    				if(i != col) {
		    					sb.append(rs.getString(i) + "\t");
		    				}else {
		    					sb.append(rs.getString(i) + "");
		    				}
		    			}
		    		}
		    	}else {
		    		sb.append("连接失败");
		    	}
		    } catch (SQLException e) {
				sb.append("连接失败");
		    }

		    System.out.println(sb.toString());
		    String fileName="D:\\Desktop\\1\\CinemaInfo.txt";
            try
            {
                    //使用这个构造函数时，如果存在kuka.txt文件，
                    //则直接往kuka.txt中追加字符串
                    FileWriter writer=new FileWriter(fileName,true);//true表示追加
                    writer.write(sb.toString());
                    writer.close();
            } catch (IOException e)
            {
                    e.printStackTrace();
            }
		}
	}
	
}
